# 魔术方法
class Student:
    __country = "中国"
    def __init__(self,name,sex,age):
        self.name = name
        self.sex = sex
        self.age = age
        print(f"创建一个{self.name}对象")
    def __str__(self):
        return f"student---name:{self.name},sex:{self.sex},age:{self.age}"
    def __lt__(self, other):
        return self.age < other.age
    def __le__(self, other):
        return self.age <= other.age
    def __eq__(self, other):
        return self.age == other.age
    def __call__(self, *args, **kwargs):
        print("对象()执行此方法")
    # 可以像list一样，使用list[下标]来获取值，下标取决于index
    def __getitem__(self, index):
        return 999
    # 可以像list一样，使用list[下标]=1来设置值
    def __setitem__(self, key, value):
        print(key, value)
    # 可以用del obj[key]
    def __delitem__(self, key):
        print(key)
    def __enter__(self):
        print("进来了")
        return 10
    def __exit__(self, exc_type, exc_val, exc_tb):
        print("出去了")
    # 可以将两个对象相加 使用+时会执行
    # obj1.__add__(obj2)
    def __add__(self, other):
        return self.name + other.name

student = Student("紫嫣", "女孩", 22)
print(student)
student1 = Student("嫣然", "女孩", 22)
print(student < student1)
print(student <= student1)
print(student == student1)
# 当类中有__call__()方法时 对象()执行此方法
student1()
# 获取对象中封装了哪些成员
print("获取对象中封装了哪些成员:",student1.__dict__)
# 获取类中有哪些成员
print("获取类中有哪些成员:",Student.__dict__)

print(student1[2])
student1[2]="萌萌"
del student1[500]

# __enter__ 返回什么  f就是什么
with student1 as f:
    print("123", f)
# 类的__add__() 可以将两个对象相加 使用+时会执行
# obj1.__add__(obj2)
print((student1 + student))